package com.indusfo.spc.client.controller;

import com.indusfo.spc.annotation.UserLoginToken;
import com.indusfo.spc.client.pojo.ClientSpcOrder;
import com.indusfo.spc.client.service.ClientSpcOrderService;
import com.indusfo.spc.pojo.OrderFlow;
import com.indusfo.spc.vo.JSONObject;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhengd
 * @create 2019-12-04
 */
@RestController
@RequestMapping("/client/spcOrder")
public class ClientSpcOrderController {

	@Autowired
	private ClientSpcOrderService clientSpcOrderService;

	/**
	 * 客户端检测单保存
	 * 
	 * @param ClientSpcOrder
	 * @return
	 */
	@PostMapping("/saveClientSpcOrder")
	@UserLoginToken
	public JSONObject saveClientSpcOrder(@RequestBody ClientSpcOrder clientSpcOrder) {
		try {
			return JSONObject.oK(clientSpcOrderService.saveClientSpcOrder(clientSpcOrder));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, e.getMessage());
		}
	}

	/**
	 * 客户端检测单更新
	 * 
	 * @param ClientSpcOrder
	 * @return
	 */
	@PostMapping("/updateClientSpcOrder")
	@UserLoginToken
	public JSONObject updateClientSpcOrder(@RequestBody ClientSpcOrder clientSpcOrder) {
		try {
			return JSONObject.oK(clientSpcOrderService.updateClientSpcOrder(clientSpcOrder));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, e.getMessage());
		}
	}

	/**
	 * 客户端检测单高级查询
	 * 
	 * @param ClientSpcOrder
	 * @return
	 */
	@PostMapping("/queryClientSpcOrder")
	@UserLoginToken
	public JSONObject queryClientSpcOrder(@RequestBody ClientSpcOrder clientSpcOrder) {
		try {
			return JSONObject.oK(clientSpcOrderService.queryClientSpcOrder(clientSpcOrder),
					clientSpcOrderService.countQueryClientSpcOrder(clientSpcOrder));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, e.getMessage());
		}
	}

	/**
	 * 根据任务单ID和工序ID查询流程ID
	 * 
	 * @param OrderFlow
	 * @return
	 */
	@PostMapping("/queryOrderFlowIdByOrderId")
	@UserLoginToken
	public JSONObject queryOrderFlowIdByOrderId(@RequestBody OrderFlow orderFlow) {
		try {
			return JSONObject.oK(clientSpcOrderService.queryOrderFlowIdByOrderId(orderFlow));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}
	}

	/**
	 * 根据检测单ID查询检测项目
	 * 
	 * @param spcOrderId
	 * @return
	 */
	@PostMapping("/queryClientSpcDete")
	@UserLoginToken
	public JSONObject queryClientSpcDete(@RequestBody Map<String, Integer> map) {
		try {
			return JSONObject.oK(clientSpcOrderService.queryClientSpcDete(map.get("spcOrderId")));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}
	}

	/**
	 * 根据检测单ID查询基本信息
	 * 
	 * @param spcOrderId
	 * @return
	 */
	@PostMapping("/queryClientSpcTest")
	@UserLoginToken
	public JSONObject queryClientSpcTest(@RequestBody Map<String, Integer> map) {
		try {
			return JSONObject.oK(clientSpcOrderService.queryClientSpcTest(map.get("spcOrderId")));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}
	}

	/**
	 * 根据任务单ID查询工序
	 * 
	 * @param orderId
	 * @return
	 */
	@PostMapping("/selectProByOrderId")
	@UserLoginToken
	public JSONObject selectProByOrderId(@RequestBody Map<String, Integer> map) {
		try {
			return JSONObject.oK(clientSpcOrderService.selectProByOrderId(map.get("orderId")));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, "系统错误，请联系管理员！");
		}
	}
	
	/**
	 * 客户端检测单完成
	 * 
	 * @param ClientSpcOrder
	 * @return
	 */
	@PostMapping("/finishClientSpcOrder")
	@UserLoginToken
	public JSONObject finishClientSpcOrder(@RequestBody ClientSpcOrder clientSpcOrder) {
		try {
			return JSONObject.oK(clientSpcOrderService.finishClientSpcOrder(clientSpcOrder));
		} catch (Exception e) {
			return JSONObject.build(JSONObject.ERROR, e.getMessage());
		}
	}
}
